/**
 * @param {String | Array}
 * @return {Image}
 *  */
function createImage(src) {
  let img;
  if (typeof src === "string") {
    img = new Image(); // 异步加载
    img.src = src;
  } else {
    img = [];
    for (let i = 0; i < src.length; i++) {
      img[i] = new Image();
      img[i].src = src[i];
    }
  }
  return img;
}

const IMAGEList = {
  bg: "./img/bg/bg1.jpg",
  logo: "./img/logo/logo.webp",
  pause: "./img/pause/pause.png",
  loadingFrame: [
    "./img/loading/loading-1.png",
    "./img/loading/loading-2.png",
    "./img/loading/loading-3.png",
    "./img/loading/loading-4.png",
    "./img/loading/loading-5.png",
    "./img/loading/loading-6.png",
    "./img/loading/loading-7.png",
    "./img/loading/loading-8.png",
  ],
  heroFrame: {
    live: ["./img/hero/hero_b_04_big.png"],
    death: [
      "./img/hero/hero_death_1.png",
      "./img/hero/hero_death_2.png",
      "./img/hero/hero_death_3.png",
    ],
  },
  bullet1Img: "./img/bullet/bullet1.png",
  bullet2Img: "./img/bullet/bullet2.png",
  e1Frame: {
    live: ["./img/enemy/e1_live_1.png"],
    death: ["./img/enemy/e1_death_1.png", "./img/enemy/e1_death_2.png"],
  },
  e2Frame: {
    live: ["./img/enemy/e2_live_1.png"],
    death: ["./img/enemy/e2_death_1.png", "./img/enemy/e2_death_2.png"],
  },
  supplyBomb: {
    live: ["./img/supply/bomb_supply.png"],
    death: ["./img/supply/supply_down4.png"],
  },
  supplyBullet: {
    live: ["./img/supply/bullet_supply.png"],
    death: ["./img/supply/supply_down4.png"],
  },
  bombExplosion: [
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt03.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt04.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt05.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt06.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt07.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
    "./img/bullet/ttt08.png",
  ],
  pauseBtn: {
    normal: "./img/button/pause_nor.png",
    pressed: "./img/button/pause_pressed.png",
    resumeNormal: "./img/button/resume_nor.png",
    resumePressed: "./img/button/resume_pressed.png",
  },
};

//   初始化背景
const bg = createImage(IMAGEList.bg); // 异步加载

//   初始化logo
const logo = createImage(IMAGEList.logo);

//   初始化暂停
const pause = createImage(IMAGEList.pause);

// 初始化loadig
const loadingFrame = createImage(IMAGEList.loadingFrame);

// 初始化飞机
const heroFrame = {
  live: createImage(IMAGEList.heroFrame.live),
  death: createImage(IMAGEList.heroFrame.death),
};

// 初始化子弹
const bullet1Img = createImage(IMAGEList.bullet1Img);
const bullet2Img = createImage(IMAGEList.bullet2Img);

// 初始化敌机
// e1
const e1Frame = {
  live: createImage(IMAGEList.e1Frame.live),
  death: createImage(IMAGEList.e1Frame.death),
};
// e2
const e2Frame = {
  live: createImage(IMAGEList.e2Frame.live),
  death: createImage(IMAGEList.e2Frame.death),
};
// 初始化补给图像
const supplyBombImg = {
  live: createImage(IMAGEList.supplyBomb.live),
  death: createImage(IMAGEList.supplyBomb.death),
};
const supplyBulletImg = {
  live: createImage(IMAGEList.supplyBullet.live),
  death: createImage(IMAGEList.supplyBullet.death),
};

// 初始化炸弹图片
const bombExplosionImgs = createImage(IMAGEList.bombExplosion);

//   天空类配置
const SkyConfig = {
  bg: bg,
  width: 650,
  height: 850,
  speed: 20,
};

//   加载类配置
const LoadingConfig = {
  bg: loadingFrame,
  width: 320,
  height: 300,
  x: 0,
  y: 850 - 200,
  speed: 100,
};

// 英雄类配置
const HeroConfig = {
  bg: heroFrame,
  width: 100, // 原始为50，放大为100
  height: 80, // 原始为40，放大为80
  speed: 400,
  shotInterval: 250,
};

// 子弹1配置
const Bullet1Config = {
  bg: bullet1Img,
  width: 14,
  height: 28,
};

// 子弹2的配置
const Bullet2Config = {
  bg: bullet2Img,
  width: 44,
  height: 50,
};

// 敌机类
const e1Config = {
  type: 1,
  frame: e1Frame,
  width: 40,
  height: 61,
  life: 1,
  score: 1,
  minSpeed: 20,
  maxSpeed: 10,
};
const e2Config = {
  type: 2,
  frame: e2Frame,
  width: 87,
  height: 58,
  life: 6,
  score: 10,
  minSpeed: 50,
  maxSpeed: 20,
};
// 初始化补给类配置
const bombSupplyConfig = {
  type: 3,
  frame: supplyBombImg,
  width: 40,
  height: 54,
  life: 1,
  score: 0,
  minSpeed: 20,
  maxSpeed: 10,
};

const bulletSupplyConfig = {
  type: 4,
  frame: supplyBulletImg,
  width: 29,
  height: 44,
  life: 1,
  score: 0,
  minSpeed: 20,
  maxSpeed: 10,
};
